package net.xiduth.game.player.content.transportation;

import net.xiduth.game.WorldTile;
import net.xiduth.game.player.Player;
import net.xiduth.game.player.content.Magic;

public class TeleportInterface {

	private final static int INTERFACE = 583;

	public static enum TeleportPlaces {
		//first row(Bosses)
		NEX(49, null, "Nex") {
			@Override
			public boolean sendOtherActions(final Player player) {
				player.setNextWorldTile(new WorldTile(2905,5203,0));
				player.stopAll();
				player.getControlerManager().startControler("GodWars");
				return true;
			}
		},
		BANDOS(50, new WorldTile(2869, 5358, 2), "Bandos(GWD)"),
		SARADOMIN(51, new WorldTile(2901, 5265, 0), "Saradomin(GWD)"),
		ARMADYL(52, new WorldTile(2826, 5305, 2), "Armadyl(GWD)"),
		
		//Second row(moar bosses)
		ZAMORAK(53, new WorldTile(2928, 5320, 2), "Zamorak(GWD)"),	
		BLINK(54, new WorldTile(86, 5575, 0), "Blink"),	
		KBD(55, new WorldTile(0, 0, 0), "King Black Dragon"),
		CORP(56, new WorldTile(2966, 4383, 2), "Corporeal Beast"),
		
		//Third row(Pk spots & Cities)
		MAJOR_CITIES(57, null, "Major Cities") {
		@Override
		public boolean sendOtherActions(final Player player) {
			player.getDialogueManager().startDialogue("MajorCitiesTeleport");
			return true;
		}
	},
		MAGE_BANK(58, new WorldTile(2539, 4712, 0), "Magebank"),
		WEST_PVP(70, new WorldTile(2984, 3596, 0), "West(PVP)"),
		MULTI_PVP(59,new WorldTile(3240, 3611, 0), "Multi PVP");

		private int componentId;
		private WorldTile tile;
		private String name;

		private TeleportPlaces(int componentId, WorldTile tile, String name) {
			this.name = name;
			this.tile = tile;
			this.componentId = componentId;
		}

		public WorldTile getWorldTile() {
			return tile;

		}

		public String getTeleportName() {
			return name;
		}

		public int getTeleportId() {
			return componentId;
		}

		public boolean sendOtherActions(final Player player) {
			return false;

		}

		private static TeleportPlaces getTele(int teleId) {
			for (TeleportPlaces tele : TeleportPlaces.values()) {
				if (tele.getTeleportId() == teleId) {
					return tele;
				}
			}
			return null;
		}

	}

	private static void unlockButtons(final Player player) {
		player.getPackets().sendConfig(1045, -1);
		player.getPackets().sendConfig(1046, -1);
		player.getPackets().sendConfig(1047, -1);
		// TODO find config that unlocks 2 more
	}

	public static void loadTeleportInterface(final Player player) {
		player.getInterfaceManager().sendInterface(INTERFACE);
		unlockButtons(player);
		player.getPackets().sendIComponentText(INTERFACE, 14,
				"Choose a Minigame...");//Title
		for (int i = 48; i < 80; i++) {
			player.getPackets() // All slot text to "not added."
				.sendIComponentText(INTERFACE, i, "Not added.");
		}
		for (TeleportPlaces tele : TeleportPlaces.values()) {
			// Changes slot text based on added teles(00FF00 = green)
			player.getPackets().sendIComponentText(INTERFACE,
					tele.getTeleportId(),
					"<col=00FF00>" + tele.getTeleportName());
		}

	}

	/**
	 * Teleports the player to the location based on the componentId
	 * 
	 * @param player
	 * @param buttonId
	 * @return true if teleport exists
	 */
	public static boolean teleport(final Player player, int buttonId) {
		TeleportPlaces location = TeleportPlaces.getTele(buttonId);
		if (location == null) {
			player.sendMessage("Location has not been added!");
			return false;
		}
		if(location.getWorldTile() != null)
		Magic.sendNormalTeleportSpell(player, 0, 0, location.getWorldTile());
		location.sendOtherActions(player);
		return true;
	}
}